package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.Constant;
import com.company.project.common.utils.DateUtils;
import com.company.project.entity.BusiPatrolEntity;
import com.company.project.entity.SysUser;
import com.company.project.service.HttpSessionService;
import com.company.project.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.BusiAlarmEntity;
import com.company.project.service.BusiAlarmService;

import javax.annotation.Resource;


/**
 * 一键报警
 *
 * @author zhanggm
 * @email *****@mail.com
 * @date 2021-06-15 09:54:41
 */
@Controller
@RequestMapping("/")
public class BusiAlarmController {
    @Autowired
    private BusiAlarmService busiAlarmService;
    @Resource
    private UserService userService;
    @Resource
    private HttpSessionService httpSessionService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/busiAlarm")
    public String busiAlarm() {
        return "busialarm/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("busiAlarm/add")
    @RequiresPermissions("busiAlarm:add")
    @LogAnnotation(title = "一键报警", action = "新增报警")
    @ResponseBody
    public DataResult add(@RequestBody BusiAlarmEntity busiAlarm){
        busiAlarmService.wholeAdd(busiAlarm);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("busiAlarm/delete")
    @RequiresPermissions("busiAlarm:delete")
    @LogAnnotation(title = "一键报警", action = "删除报警")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        busiAlarmService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("busiAlarm/update")
    @RequiresPermissions("busiAlarm:update")
    @LogAnnotation(title = "一键报警", action = "更新报警")
    @ResponseBody
    public DataResult update(@RequestBody BusiAlarmEntity busiAlarm){
        busiAlarmService.updateById(busiAlarm);
        return DataResult.success();
    }

    @ApiOperation(value = "阅读")
    @PutMapping("busiAlarm/read")
    @RequiresPermissions("busiAlarm:update")
    @LogAnnotation(title = "一键报警", action = "阅读数据")
    @ResponseBody
    public DataResult read(@RequestBody BusiAlarmEntity busiAlarm){
        busiAlarm.setReadStatus(Constant.READ);
        busiAlarm.setUpdateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        busiAlarmService.updateById(busiAlarm);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("busiAlarm/listByPage")
    @RequiresPermissions("busiAlarm:list")
    @LogAnnotation(title = "一键报警", action = "查询分页数据")
    @ResponseBody
    public DataResult findListByPage(@RequestBody BusiAlarmEntity busiAlarm){
        Page page = new Page(busiAlarm.getPage(), busiAlarm.getLimit());
        LambdaQueryWrapper<BusiAlarmEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(BusiAlarmEntity::getId, busiAlarm.getId());
        // 如果是APP用户，则只能查询到自己的数据.如果是工作人员或系统管理员，则可以查看所有数据
        String userId= httpSessionService.getCurrentUserId();
        SysUser sysUser = userService.getById(userId);
        // 查看数据来源是否为APP
        if (Constant.APP.equals(sysUser.getCreateWhere())) {
            queryWrapper.eq(BusiAlarmEntity::getUserId, sysUser.getId());
        }

        // 起始时间
        String startTime = busiAlarm.getStartTime();
        String endTime = busiAlarm.getEndTime();
        if (StringUtils.isNotBlank(startTime)) {
            queryWrapper.gt(BusiAlarmEntity::getCreateTime, startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            queryWrapper.lt(BusiAlarmEntity::getCreateTime, endTime);
        }
        queryWrapper.orderByDesc(BusiAlarmEntity::getCreateTime);

        IPage<BusiAlarmEntity> iPage = busiAlarmService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
